package com.qfhd.soft.cjzcore.UI.CJBitmapHelper;

import android.graphics.Bitmap;

import com.qfhd.soft.cjzcore.Core.CJFile;
import com.qfhd.soft.cjzcore.Core.CJLogger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CJBitmapHelper {

    static CJBitmapHelper bitmapHelper;
    public static CJBitmapHelper getInstance(){
        if(bitmapHelper == null){
            synchronized (CJBitmapHelper.class){
                bitmapHelper = new CJBitmapHelper();
            }
        }
        return bitmapHelper;
    }

    public Bitmap compressBitmap(Bitmap bitmap){
        CJLogger.v("压缩前图片的大小" + (bitmap.getByteCount() / 1024 / 1024) + "KB宽度为"
                + bitmap.getWidth() + "高度为" + bitmap.getHeight());
        //以宽为准 等比例缩放高
        float rate_w =  720.0f / bitmap.getWidth();
        bitmap = Bitmap.createScaledBitmap(bitmap, 720, (int) (bitmap.getHeight()*rate_w), true);
        CJLogger.v("压缩后图片的大小" + (bitmap.getByteCount() / 1024 / 1024) + "KB宽度为"
                + bitmap.getWidth() + "高度为" + bitmap.getHeight());
        return bitmap;
    }

    public String getSavePath(String name){
        return CJFile.getInstance().getDataFolder() + name + ".jpg";
    }

    /** 保存方法 */
    public boolean saveBitmap(Bitmap bm,String name) {
        boolean ret = false;
        File f = new File(CJFile.getInstance().getDataFolder(), name+".jpg");
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
            CJLogger.v("saveBitmap sucess");
            ret = true;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            CJLogger.v("saveBitmap failed:" + e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            CJLogger.v("saveBitmap failed:" + e.toString());
        }

        return ret;
    }
}
